package com.info.zhiduoduo.repository.mapper.company;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.company.CompanyZhaoshangSubLedgerIncomingRecordDto;
import com.info.zhiduoduo.repository.entity.company.CompanyZhaoshangSubLedgerIncomingRecord;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

public interface CompanyZhaoshangSubLedgerIncomingRecordMapper extends BaseMapper<CompanyZhaoshangSubLedgerIncomingRecord> {

	@Select(" select * from zdd_company_zhaoshang_sub_ledger_incoming_record " +
			" where if(#{compid} = 0, 1=1, compid = #{compid}) " +
			" and if(#{ledgerId} = 0, 1=1, ledger_id = #{ledgerId}) " +
			" and trxdate = #{date} " +
			" order by id desc ")
	List<CompanyZhaoshangSubLedgerIncomingRecord> listAllByCompidLedgerIdDate(int compid, int ledgerId, Date date);

	@Select(" select * from zdd_company_zhaoshang_sub_ledger_incoming_record " +
			" where ledger_id = #{ledgerId} " +
			" and state in (1, 5) " +
			" and operation_type = 1")
	List<CompanyZhaoshangSubLedgerIncomingRecord> listAllPending(int ledgerId);

	@Select(" select czslir.*,pm.name as merchantName,czsl.ledger_name " +
			" from zdd_company_zhaoshang_sub_ledger_incoming_record czslir " +
			" join zdd_company_zhaoshang_sub_ledger czsl on czsl.id = czslir.ledger_id " +
			" join zdd_payment_merchant pm on pm.id = czsl.merchant_id " +
			" where if(#{compid} = 0, 1=1, czslir.compid = #{compid}) " +
			" and czsl.ledger_name like '%${compname}%' " +
			" and if(#{merchantId} = 0, 1=1, czslir.merchant_id = #{merchantId}) " +
			" and czslir.trxnbr like '%${orderNo}%' " +
			" and czslir.trxdate between #{from} and #{to} " +
			" and if(#{state} = 0, 1=1, czslir.state = #{state}) " +
			" and if(#{operationType}=0, 1=1, operation_type = #{operationType}) " +
			" order by czslir.id desc")
	List<CompanyZhaoshangSubLedgerIncomingRecordDto> list(int compid, String compname, int merchantId, String orderNo,
	                                                      int state, int operationType, Date from, Date to);

	@Update(" update zdd_company_zhaoshang_sub_ledger_incoming_record set state = 4 where recharge_id = #{rechargeId}")
	int rechargeSuccessed(Integer rechargeId);

	@Select(" select * from zdd_company_zhaoshang_sub_ledger_incoming_record " +
			" where recharge_id = #{rechargeId} " +
			" order by id desc ")
	List<CompanyZhaoshangSubLedgerIncomingRecordDto> listByRechargeId(Integer rechargeId);
}
